package EBM.DB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import EBM.model.BugLog;

public class BugLogDAO {
	private List<Object> response;
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	//private String sql;
	private int result;

	public BugLogDAO(){
		response = null;
		conn = null;
		ps = null;
		rs = null;
		//sql = null;
		result = 0;
	}

	public boolean save(Object proid, BugLog bugLog) 
	throws SQLException, ClassNotFoundException{

		String sql="insert into bug_log_" + proid + " "+
		"(time,person,detail,bugid) values(?,?,?,?)";

		conn = DBConnection.getConn();
		ps = conn.prepareStatement(sql);
		ps.setObject(1, bugLog.getTime());
		ps.setObject(2, bugLog.getPerson());
		ps.setObject(3, bugLog.getDetail());
		ps.setObject(4, bugLog.getBugid());
		
		response = new ArrayList<Object>();

		result = ps.executeUpdate();
		conn.close();

		if(result>0){
			return true;
		}else{
			return false;
		}
	}
	
	public List<Object> findByBugid(Object proid, Object bugid) 
	throws SQLException, ClassNotFoundException{

		String sql="select * from bug_log_" + proid + " where bugid=?";

		conn = DBConnection.getConn();
		ps = conn.prepareStatement(sql);
		ps.setObject(1, bugid);
		
		response = new ArrayList<Object>();

		rs = ps.executeQuery();	

		if(rs.next()){
			do{
				BugLog bl = new BugLog();
				
				//bl.setBugid(rs.getString("id"));
				bl.setId(rs.getString("id"));
				bl.setTime(rs.getString("time"));
				bl.setPerson(rs.getString("person"));
				bl.setDetail(rs.getString("detail"));
				bl.setBugid(rs.getString("bugid"));
				
				response.add(bl);
			}while(rs.next());
		}else{
			response.add("-1");
		}	
		conn.close();
		return response;
	}
}
